﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XMLGener.Data;
using XMLGener.Utils;

namespace XMLGener.GUI
{
    /// <summary>
    /// This component is used for editing data type restrictions
    /// </summary>
    public partial class XDataTypePanel : UserControl
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public XDataTypePanel()
        {
            InitializeComponent();
            CustomInitialize();
        }

        private List<NumericUpDown> numEditors = null;
        private List<TextBox> txtEditors = null;
        private Dictionary<CheckBox, Control> bindAbility = null;

        /// <summary>
        /// Custom initialization
        /// </summary>
        private void CustomInitialize()
        {
            numEditors = new List<NumericUpDown>();
            txtEditors = new List<TextBox>();
            numEditors.Add(nLength);
            numEditors.Add(nMaxExclusive);
            numEditors.Add(nMaxInclusive);
            numEditors.Add(nMaxLength);
            numEditors.Add(nMinExclusive);
            numEditors.Add(nMinInclusive);
            numEditors.Add(nMinLength);
            txtEditors.Add(tbPattern);
            txtEditors.Add(tbEnumeration);

            bindAbility = new Dictionary<CheckBox, Control>();
            bindAbility.Add(cbEnumeration   , tbEnumeration);
            bindAbility.Add(cbLength        , nLength);
            bindAbility.Add(cbMaxExclusive  , nMaxExclusive);
            bindAbility.Add(cbMaxInclusive  , nMaxInclusive);
            bindAbility.Add(cbMaxLength     , nMaxLength);
            bindAbility.Add(cbMinExclusive  , nMinExclusive);
            bindAbility.Add(cbMinInclusive  , nMinInclusive);
            bindAbility.Add(cbMinLength     , nMinLength);
            bindAbility.Add(cbPattern       , tbPattern);
        }
        

        private XDataType dataType = null;

        /// <summary>
        /// set currently edited data type
        /// </summary>
        /// <param name="value"></param>
        public void setDataType(XDataType value)
        {
            clearAllBinds();
            clearComponents();
            this.dataType = value;
            bindComponents();
            refreshComponentsAbility();
        
        }

        private void clearAllBinds()
        {
            foreach (NumericUpDown numEdit in numEditors)
                numEdit.DataBindings.Clear();
            foreach (TextBox txtEdit in txtEditors)
                txtEdit.DataBindings.Clear();
            foreach (CheckBox cb in bindAbility.Keys)
                cb.DataBindings.Clear();
        }


        private void bindComponents()
        { 
            if (this.dataType==null)
                    return;


            ignoreRefreshing = true;
            // set interited patterns
            nMinLength.Minimum    = nMaxLength.Minimum    = nLength.Minimum = dataType.minLengthInherited;
            nMinLength.Maximum    = nMaxLength.Maximum    = nLength.Maximum = dataType.maxLengthInherited;
            nMinInclusive.Minimum = nMaxInclusive.Minimum = dataType.minInclusiveInherited;

            nMinInclusive.Maximum = nMaxInclusive.Maximum = dataType.maxInclusiveInherited;
            nMinExclusive.Minimum = nMaxExclusive.Minimum = dataType.minExclusiveInherited;
            nMinExclusive.Maximum = nMaxExclusive.Maximum = dataType.maxExclusiveInherited;

            dataType.validateData();

            nLength.DataBindings.Add("Value", dataType, "length", false, DataSourceUpdateMode.OnPropertyChanged);
            nMinLength.DataBindings.Add("Value", dataType, "minLength", false, DataSourceUpdateMode.OnPropertyChanged);
            nMaxLength.DataBindings.Add("Value", dataType, "maxLength", false, DataSourceUpdateMode.OnPropertyChanged);
            nMinInclusive.DataBindings.Add("Value", dataType, "minInclusive", false, DataSourceUpdateMode.OnPropertyChanged);
            nMaxInclusive.DataBindings.Add("Value", dataType, "maxInclusive", false, DataSourceUpdateMode.OnPropertyChanged);
            nMinExclusive.DataBindings.Add("Value", dataType, "minExclusive", false, DataSourceUpdateMode.OnPropertyChanged);
            nMaxExclusive.DataBindings.Add("Value", dataType, "maxExclusive", false, DataSourceUpdateMode.OnPropertyChanged);
            tbPattern.DataBindings.Add("Text", dataType, "pattern", false, DataSourceUpdateMode.OnPropertyChanged);
            tbEnumeration.DataBindings.Add("Text", dataType, "enumeration");

            cbEnumeration.DataBindings.Add("Checked", dataType, "useEnumeration", false, DataSourceUpdateMode.OnPropertyChanged);
            cbLength.DataBindings.Add("Checked", dataType, "useLength",false,DataSourceUpdateMode.OnPropertyChanged);
            cbMaxExclusive.DataBindings.Add("Checked", dataType, "useMaxExclusive", false, DataSourceUpdateMode.OnPropertyChanged);
            cbMaxInclusive.DataBindings.Add("Checked", dataType, "useMaxInclusive", false, DataSourceUpdateMode.OnPropertyChanged);
            cbMaxLength.DataBindings.Add("Checked", dataType, "useMaxLength", false, DataSourceUpdateMode.OnPropertyChanged);
            cbMinLength.DataBindings.Add("Checked", dataType, "useMinLength", false, DataSourceUpdateMode.OnPropertyChanged);
            cbMinExclusive.DataBindings.Add("Checked", dataType, "useMinExclusive", false, DataSourceUpdateMode.OnPropertyChanged);
            cbMinInclusive.DataBindings.Add("Checked", dataType, "useMinInclusive", false, DataSourceUpdateMode.OnPropertyChanged);
            cbPattern.DataBindings.Add("Checked", dataType, "usePattern", false, DataSourceUpdateMode.OnPropertyChanged);

            ignoreRefreshing = false;

            //refreshComponentsAbility();

           /* cbEnumeration.Enabled  = !dataType.useEnumerationInherited;
            cbLength.Enabled       = !dataType.useLengthInherited;
            cbMaxExclusive.Enabled = !dataType.useMaxExclusiveInherited;
            cbMaxInclusive.Enabled = !dataType.useMaxInclusiveInherited;
            cbMaxLength.Enabled    = !dataType.useMaxLengthInherited;
            cbMinExclusive.Enabled = !dataType.useMinExclusiveInherited;
            cbMinInclusive.Enabled = !dataType.useMinInclusiveInherited;
            cbMinLength.Enabled    = !dataType.useMinLengthInherited;
            cbPattern.Enabled      = !dataType.usePatternInherited;*/

        }
        private void clearComponents()
        {
            foreach (NumericUpDown numEdit in numEditors)
                numEdit.Value = 0;
            foreach (TextBox txtEdit in txtEditors)
                txtEdit.Text = "";
            foreach (CheckBox cb in bindAbility.Keys)
            {
                bindAbility[cb].Enabled = false;
                cb.Checked = false;
            }
        }

        private bool ignoreRefreshing = false;
        private void refreshComponentsAbility()
        {
            if (ignoreRefreshing) return;
            foreach (CheckBox cb in bindAbility.Keys)
            {
                bindAbility[cb].Enabled = cb.Checked;
            }
        }

        

        private void cb_checkChange(object sender, EventArgs e)
        {
            refreshComponentsAbility();
        }

        private void nMaxInclusive_ValueChanged(object sender, EventArgs e)
        {

        }

       

        


        

    }
}
